package io.operator;

/**
 * & 与；| 或；~ 非（也叫做求反）；^ 异或
 * 
 * @author Administrator
 * 
 */
public class LogicOperator {

	public static final String INT_ZENO = "00000000000000000000000000000000";

	public static void main(String[] args) {
		operator_1();
		operator_2();
		operator_3();
		operator_4();
	}

	/**
	 * 两个都为 1时才为1，否则为0
	 */
	public static void operator_1() {
		System.out.println("与运算符(&)");
		int first_1 = 5;
		int second_1 = 3;
		System.out.println(first_1 + ":"
				+ getFullBinary(Integer.toBinaryString(first_1)));
		System.out.println(second_1 + ":"
				+ getFullBinary(Integer.toBinaryString(second_1)));
		int result_1 = 5 & 3;
		System.out.println(result_1 + ":"
				+ getFullBinary(Integer.toBinaryString(result_1)));

		int first_2 = -5;
		int second_2 = 3;
		System.out.println(first_2 + ":"
				+ getFullBinary(Integer.toBinaryString(first_2)));
		System.out.println(" " + second_2 + ":"
				+ getFullBinary(Integer.toBinaryString(second_2)));
		int result_2 = -5 & 3;
		System.out.println(" " + result_2 + ":"
				+ getFullBinary(Integer.toBinaryString(result_2)));
	}

	/**
	 * 只要有一个为1，结果为1
	 */
	public static void operator_2() {
		System.out.println("或运算符(|)");
		int first_1 = 5;
		int second_1 = 3;
		System.out.println(first_1 + ":"
				+ getFullBinary(Integer.toBinaryString(first_1)));
		System.out.println(second_1 + ":"
				+ getFullBinary(Integer.toBinaryString(second_1)));
		int result_1 = 5 | 3;
		System.out.println(result_1 + ":"
				+ getFullBinary(Integer.toBinaryString(result_1)));

		int first_2 = -5;
		int second_2 = 3;
		System.out.println(first_2 + ":"
				+ getFullBinary(Integer.toBinaryString(first_2)));
		System.out.println(" " + second_2 + ":"
				+ getFullBinary(Integer.toBinaryString(second_2)));
		int result_2 = -5 | 3;
		System.out.println(result_2 + ":"
				+ getFullBinary(Integer.toBinaryString(result_2)));
	}

	/**
	 * 1变为0,0变为1
	 */
	public static void operator_3() {
		System.out.println("非运算符(~)");
		int first_1 = 5;
		System.out.println(" " + first_1 + ":"
				+ getFullBinary(Integer.toBinaryString(first_1)));
		int result_1 = ~5;
		System.out.println(result_1 + ":"
				+ getFullBinary(Integer.toBinaryString(result_1)));
	}

	/**
	 * 两个相同时为1，不同时为0
	 */
	public static void operator_4() {
		System.out.println("异或运算符(^)");
		int first_1 = 5;
		int second_1 = 3;
		System.out.println(first_1 + ":"
				+ getFullBinary(Integer.toBinaryString(first_1)));
		System.out.println(second_1 + ":"
				+ getFullBinary(Integer.toBinaryString(second_1)));
		int result_1 = 5 ^ 3;
		System.out.println(result_1 + ":"
				+ getFullBinary(Integer.toBinaryString(result_1)));

		int first_2 = -5;
		int second_2 = 3;
		System.out.println(first_2 + ":"
				+ getFullBinary(Integer.toBinaryString(first_2)));
		System.out.println(" " + second_2 + ":"
				+ getFullBinary(Integer.toBinaryString(second_2)));
		int result_2 = -5 ^ 3;
		System.out.println(result_2 + ":"
				+ getFullBinary(Integer.toBinaryString(result_2)));
	}

	private static String getFullBinary(String binaryString) {
		return INT_ZENO.substring(binaryString.length()) + binaryString;
	}

}
